#include "controllerserialport.h"

ControllerSerialPort::ControllerSerialPort(QObject *parent)
    : QObject(parent)
{
    serialPort = new QSerialPort();
    if(open())
    {
        emit connected(true);
    }
    isReadHalf = false;
}

ControllerSerialPort::~ControllerSerialPort()
{
    close();
}

bool ControllerSerialPort::open()
{
    QSettings settings("configs.ini",QSettings::IniFormat);
    QString port = settings.value("port").toString();
    QSerialPortInfo comInfo;
    foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
    {
        if(info.portName() == port)
        {
            comInfo = info;
            break;
        }
    }
    if(comInfo.isNull())
    {
        return false;
    }
    serialPort->setPort(comInfo);
    serialPort->setBaudRate(settings.value("baudrate").toInt());
    serialPort->setDataBits(QSerialPort::Data8);
    serialPort->setParity(QSerialPort::NoParity);
    serialPort->setStopBits(QSerialPort::OneStop);
    serialPort->setFlowControl(QSerialPort::NoFlowControl);

    if(!serialPort->open(QIODevice::ReadWrite))
    {
        return false;
    }
    connect(serialPort,&QSerialPort::readyRead,this,&ControllerSerialPort::readyRead);
    return true;
}

bool ControllerSerialPort::close()
{
    if(serialPort->isOpen())
    {
        emit connected(false);
        serialPort->close();
    }
}

void ControllerSerialPort::process()
{
    //emit finished();
}

void ControllerSerialPort::readyRead()
{
    int length = serialPort->bytesAvailable();
    QByteArray readDateBytes = serialPort->readAll();
    while (length > 0) {
        if(isReadHalf)
        {
            isReadHalf = false;
            readHalfBuffer.append(readDateBytes);
            readDateBytes.clear();
            readDateBytes.append(readHalfBuffer);
            readHalfBuffer.clear();
            length = readDateBytes.count();
        }
        if(readDateBytes[0] == 0xAA)
        {
            if(length >= 3)
            {
                int byteLength = (int)readDateBytes[2];
                if(length == (byteLength+6))
                {
                    if(readDateBytes[length-1] == 0xEE)
                    {
                        emit analyzeData(readDateBytes);
                    }
                    else {
                        qCritical()<<"read error data : 1!";
                        return;
                    }
                    length = 0;
                }
                else if(length == (byteLength+4))
                {
                    if(readDateBytes[length-1] == 0xEE)
                    {
                        emit analyzeData(readDateBytes);
                    }
                    else {
                        qCritical()<<"read error data : 2!";
                        length = 0;
                        return;
                    }
                    length = 0;
                }
                else if(length < (byteLength+6))
                {
                    //qCritical()<<"read error data : 3!"<<byteLength<<length;
                    isReadHalf = true;
                    readHalfBuffer.append(readDateBytes);
                    length = 0;
                    return;
                }
                else if(length > (byteLength+6))
                {
                    QByteArray longdata = readDateBytes.remove(0,byteLength+6);
                    //qDebug()<<"longdata"<<longdata;
                    if(longdata[byteLength+5] == 0xEE)
                    {
                        emit analyzeData(longdata);
                    }
                    else
                    {
                        length = 0;
                        return;
                    }
                    //qCritical()<<"read error data : 大于接收字节!"<<length<<byteLength<<readDateBytes;
                    length -= (byteLength+6);
                }
                else
                {
                    length = 0;
                    qCritical()<<"read error data : 21!";
                }
            }
            else
            {
                length = 0;
                qCritical()<<"read error data : 22!";
            }
        }
        else
        {
            length = 0;
            qCritical()<<"read error data : 23!";
        }
    }
}

void ControllerSerialPort::openDoor()
{
    if(!serialPort->isOpen())
        return;
    QByteArray writeData;
    writeData.resize(8);
    writeData[0] = 0xAA;
    writeData[1] = 0x05;
    writeData[2] = 0x02;
    writeData[3] = 0xC2;
    writeData[4] = 0x0;
    writeData[7] = 0xEE;
    unsigned char crc[8];
    for(int i=0; i<4; i++)
    {
        crc[i] = writeData[1+i];
    }
    unsigned short crc_16 = crc16_ccitt(crc, 4);
    writeData[5] = crc_16>>8;
    writeData[6] = crc_16;
    serialPort->write(writeData);
}

void ControllerSerialPort::closeDoor()
{
    if(!serialPort->isOpen())
        return;
    QByteArray writeData;
    writeData.resize(8);
    writeData[0] = 0xAA;
    writeData[1] = 0x05;
    writeData[2] = 0x02;
    writeData[3] = 0xC2;
    writeData[4] = 0x01;
    writeData[7] = 0xEE;
    unsigned char crc[8];
    for(int i=0; i<4; i++)
    {
        crc[i] = writeData[1+i];
    }
    unsigned short crc_16 = crc16_ccitt(crc, 4);
    writeData[5] = crc_16>>8;
    writeData[6] = crc_16;
    serialPort->write(writeData);
}


